/**
 * 
 */
package com.mobilottery.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.TreeSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
	@author amit, yariv fedor
	Stores game participation details
 **/
@Entity
@Table(name = "Game_Participation")
public class GameParticipation {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @ManyToOne(cascade=CascadeType.ALL)
    private Game game;
    @ManyToOne
    private User user;
    @Column(name = "TRANSACTION_ID")
    private Long transactionId;
    @Column(name = "PARTICIPATION_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date participationDate;
    @Column(name = "WINING_AMOUNT")
    private Float winningAmount;
    @Column(name = "GUESSED_NUMBERS_RAW")
    private String guessedNumbersRaw;
    @Transient
    private Set<Long> guessedNumbers;

    public GameParticipation() {
    }

    public GameParticipation(Game game, User user, Date participationDate, List<Long> guessedNumbers) {
        this.game = game;
        this.user = user;
        this.participationDate = participationDate;
        setGuessedNumbers(guessedNumbers);
    }
    
    public Game getGame() {
        return game;
    }

    public String getGuessedNumbersRaw() {

        if (guessedNumbersRaw == null && guessedNumbers!=null) {

            if (guessedNumbers.size() > 0) {
                int i = 0;
                for (Long guessedNumber : this.guessedNumbers) {
                    this.guessedNumbersRaw += String.valueOf(guessedNumber.longValue());
                    if ((i + 1) != guessedNumbers.size()) {
                        this.guessedNumbersRaw += ",";
                    }
                    ++i;
                }
            }
        }
        return guessedNumbersRaw;
    }

    public Set<Long> getGuessedNumbers() {
        if (guessedNumbers == null) {
            String guessedNumbersRawStr = getGuessedNumbersRaw();
            guessedNumbers = new TreeSet<Long>();
            if (guessedNumbersRawStr != null && guessedNumbersRawStr.isEmpty() == false) {
                StringTokenizer tk = new StringTokenizer(guessedNumbersRawStr, ",");
                while (tk.hasMoreTokens()) {
                    guessedNumbers.add(Long.parseLong(tk.nextToken()));
                }
            }
        }
        return guessedNumbers;
    }

    public Long getId() {
        return id;
    }

    public Date getParticipationDate() {
        return participationDate;
    }

    public Long getTransactionId() {
        return transactionId;
    }

    public User getUser() {
        return user;
    }

    public Float getWinningAmount() {
        return  (winningAmount == null) ? -1 : winningAmount ;
    }

    public void setGame(Game game) {
        this.game = game;
    }

    protected void setGuessedNumbersRaw(String guessedNumbersRaw) {
        this.guessedNumbersRaw = guessedNumbersRaw;
    }

    public void setGuessedNumbers(List<Long> guessedNumbers) {

        this.guessedNumbers = new TreeSet<Long>(guessedNumbers);
        if(guessedNumbers!=null && !guessedNumbers.isEmpty())
        {
        	guessedNumbersRaw = "";
	        int i = 0;
	        for (Long guessedNumber : guessedNumbers) {
	            this.guessedNumbersRaw += String.valueOf(guessedNumber.longValue());
	            if ((i + 1) != guessedNumbers.size()) {
	                this.guessedNumbersRaw += ",";
	            }
	            ++i;
	        }
        }
        else
        {
        	guessedNumbers = null;
        	guessedNumbersRaw = null;
        }
    }


    public void setGuessedNumbers(String guessedNumbersRawStr) {

        if (guessedNumbersRawStr != null && !guessedNumbersRawStr.isEmpty()) {
        	List<Long> guessedNumbers = new ArrayList<Long>();
            StringTokenizer tk = new StringTokenizer(guessedNumbersRawStr, ",");
            while (tk.hasMoreTokens()) {
                guessedNumbers.add(Long.parseLong(tk.nextToken()));
            }
            this.guessedNumbersRaw = guessedNumbersRawStr;
            this.guessedNumbers = new TreeSet<Long>(guessedNumbers);
        }
        else
        {
        	guessedNumbers = null;
        	guessedNumbersRaw = null;
        }
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setParticipationDate(Date participationDate) {
        this.participationDate = participationDate;
    }

    public void setTransactionId(Long transactionId) {
        this.transactionId = transactionId;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void setWinningAmount(Float winningAmount) {
        this.winningAmount = winningAmount;
    }
}
